package com.kevin.Code.Array;

import java.util.HashMap;

/**
 * @author Vinlee Xiao
 * @Classname Leetcode_128
 * @Description Leetcode 217 存在重复的元素  简单题
 * @Date 2021/12/6 21:37
 * @Version 1.0
 */
public class Leetcode_217 {
    public static void main(String[] args) {
        Leetcode_217 m = new Leetcode_217();
        m.containsDuplicate(new int[]{1, 2, 3, 1});
    }

    /**
     * 空间O(n)
     * 时间O(n)
     *
     * @param nums
     * @return
     */
    public boolean containsDuplicate(int[] nums) {

        HashMap<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int index = nums[i];
            if (map.getOrDefault(index, 0) > 0) {
                return true;
            }
            map.put(index, map.getOrDefault(index, 0) + 1);
        }

        return false;
    }
}
